package net.siufung.market.base.dept.controller.regulation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import net.siufung.core.exception.BizServiceException;
import net.siufung.core.model.regulation.AbstractRegulation;
import net.siufung.core.utils.StringPool;
import net.siufung.market.base.dept.mapper.IDeptMapper;
import net.siufung.market.base.dept.mapper.entity.Dept;
import net.siufung.market.base.dept.api.IDeptUserApi;

import java.util.Objects;

/**
 * @author 陈建峰
 * @since 2022/7/31 10:49 下午
 */
@AllArgsConstructor
public class DeptRemoveBatchRegulation extends AbstractRegulation<String[]> {

    private static final long serialVersionUID = 6524699392605387933L;

    private final IDeptMapper deptMapper;
    private final IDeptUserApi deptUserProvider;

    @Override
    public boolean isSatisfiedBy(String[] ids) {
        if(!Long.valueOf(StringPool.ZERO).equals(
                deptMapper.selectCount(
                        new LambdaQueryWrapper<Dept>().in(Dept::getParentId, Lists.newArrayList(ids))))){
            throw new BizServiceException("部门集合存在子部门, 不能删除！");
        }
        if(Objects.nonNull(deptUserProvider) && deptUserProvider.existUsedDept(ids)){
            throw new BizServiceException("部门集合存在用户，不能删除！");
        }
        return Boolean.TRUE;
    }
}
